package smartq.app;


import java.sql.ResultSet;
import java.util.ArrayList;

import smartq.DB.Connect;
import smartq.DB.Customers;

import com.app.smart_q.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;


public class SmartQ_info_Activity extends Activity {

	
	private String SessionUsrOID = "100201301220001";
	
	Connect con = new Connect(); 
	ResultSet rs;
	String SexS;
	String   CustomerFirstName
			,CustomerLastName
			,Customersex
			,CustomerClass
			,CustomerPhonenumber,
			OID;
	
	public String getOID() {
		return OID;
	}

	public void setOID(String oID) {
		OID = oID;
	}

	//-------GET_SET-----------------------------------//
	private EditText EdPhone,EdFName,EdLName,EdClass;
	
	myDBClass myDb = new myDBClass(this);
	
	ArrayList<String> SpnSex= new ArrayList<String>();
	
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.smartq_info);
		 EdPhone = (EditText) findViewById(R.id.editText_TimeCustomer);
		 EdFName = (EditText) findViewById(R.id.editText_TimeNow);
		 EdLName = (EditText) findViewById(R.id.insertq_numbers);
		 EdClass = (EditText) findViewById(R.id.editText_Class);
		
		Bundle bundle = getIntent().getExtras();
		 CustomerPhonenumber = (String) bundle.get("CustomerPhonenumber");
		 CustomerFirstName = (String) bundle.get("CustomerFirstName");
		 CustomerLastName = (String) bundle.get("CustomerLastName");
		 Customersex = (String) bundle.get("Customersex");
		 CustomerClass = (String) bundle.get("CustomerClass");
		 
		
		
		EdPhone.setText(CustomerPhonenumber);
		EdFName.setText(CustomerFirstName);
		EdLName.setText(CustomerLastName);
		Sex();
		EdClass.setText(CustomerClass);
		
		// Watch for button clicks.
        Button button = (Button)findViewById(R.id.smartq_insertQ);
        button.setOnClickListener(buttonInfoUpdate);
        
        button = (Button)findViewById(R.id.smartq_cancle);
        button.setOnClickListener(buttonInfoDel);

	}
	
	public boolean CheckUser(){
		rs = null;
		String StrSql = "SELECT * FROM Customers WHERE CustomerPhonenumber ='"+CustomerPhonenumber+"' " +
				"AND IsDeleted= '0' ";
		rs = con.selectData(StrSql);
		try {
			if (rs.next()) {
				setOID(rs.getString("OID"));
				return true;
			}
		}catch (Exception e){
		}
		return false;
	}
    
	private OnClickListener buttonInfoUpdate = new OnClickListener() {
        public void onClick(View v) {
			rs = null;
			if(CheckUser()){
				Customers usr = new Customers(
						getOID(), 
						SessionUsrOID,
						"1", 
						null, 
						EdPhone.getText().toString(),
						EdFName.getText().toString(), 
						EdLName.getText().toString(),
						SexS, 
						EdClass.getText().toString(),
						"",
						"0",
						"0");
				try {
					if (usr.OperationData("Update") != null) {
						// Display
						myDb.DeleteData("1");
						if(Adddata(EdPhone.getText().toString())){
							restartFirstActivity();
						}
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}else{
				Customers usr = new Customers(
						null, 
						SessionUsrOID,
						"1", 
						null, 
						EdPhone.getText().toString(),
						EdFName.getText().toString(), 
						EdLName.getText().toString(),
						SexS, 
						"Class B",
						"",
						"0",
						"0");
				try {
					if (usr.OperationData("Insert") != null) {
						// Display
						myDb.DeleteData("1");
						if(Adddata(EdPhone.getText().toString())){
							restartFirstActivity();
						}
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}
			
			
		}
            
    }; 
    
    private OnClickListener buttonInfoDel = new OnClickListener() {
        public void onClick(View v) {
//        	mHandler.removeCallbacks(mCheckDB);
        	myDb.DeleteData("1");
        	restartFirstActivity();
        }
    }; 
  
    
    
	private void restartFirstActivity() {
		Intent i = getBaseContext().getPackageManager()
				.getLaunchIntentForPackage(getBaseContext().getPackageName());
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(i);
	}
	
	public Boolean Adddata(String PhonenumberCh){
		final myDBClass myDb = new myDBClass(this);
		
		long saveStatus = myDb.InsertData("1",
				PhonenumberCh,
				PhonenumberCh);
    	if(saveStatus <=  0)
    	{
            return false;
    	}
		return true;
	}
	
	
	private void Sex() {
		
		final Spinner Sex = (Spinner) findViewById(R.id.Spinner_Sex);
		SpnSex.add(Customersex);
		if(Customersex.equals("หญิง")){
			SpnSex.add("ชาย");
		}else if(Customersex.equals("ชาย")){
			SpnSex.add("หญิง");
		}else{
			SpnSex.add("ชาย");
			SpnSex.add("หญิง");
		}
		
		
			
				ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,SpnSex);
				empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
				Sex.setAdapter(empolyeeAdapter);
				
				Sex.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

		            public void onItemSelected(AdapterView<?> adapterView, 
		          	View view, int i, long l) { 
		            	SexS = SpnSex.get(i);
		            }
		            public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub
	    				Toast.makeText(SmartQ_info_Activity.this,
	             				String.valueOf("Your Selected Empty"),
	             					Toast.LENGTH_SHORT).show();
					} 

	      });
	}
	
	 @Override
		public void onBackPressed() {
		  
		        new AlertDialog.Builder(this)
		        .setTitle("ออกจากโปรแกรม")
		        .setMessage("ต้องการออกจากโปรแกรม")
		                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

		                    public void onClick(DialogInterface arg0, int arg1) {
		                        finish();
		                        System.exit(0);
		                    }
		                })
		                .setPositiveButton(android.R.string.no, null).create().show();
		    

		} 

}
